<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <header th:replace="header::html"></header>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>部门号
                    </label>
                    <div class="layui-input-block">
                        <input type="text" id="deptCode"  name="deptCode" th:value="${dept.deptCode}" required="" placeholder="请输入部门号" lay-verify="deptCode" autocomplete="off" class="layui-input"  readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门名</label>
                    <div class="layui-input-block">
                        <input type="text" id="deptName"  name="name" th:value="${dept.name}"   required="" placeholder="请输入部门名称" lay-verify="deptName" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="dept-edit">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script>
    layui.use(['form','jquery'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        form.verify({
            deptCode: function (value) {
                if (value.length > 3) {
                    return '部门号不能多于3个字符';
                }
            },
            deptName: function (value) {
                if(value.length < 2 || value.length >11){
                    return '部门名称不能不能少于2个字符多于11个字符';
                }
            }
        })

        form.on('submit(dept-edit)',function (data) {
            const json = JSON.stringify(data.field)
            $.ajax({
                url:'/cy/dept',
                data:json,
                dataType:'json',
                contentType:'application/json',
                type:'put',
                success:function(result){
                    if(result.success){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.location.reload();
                        });
                    }else{
                        layer.msg(result.msg,{icon:2,time:1000});
                    }
                }
            })
            return false;
        })
    })
</script>
<script>
</script>
</body>
</html>